package leetcode.problems;

import org.junit.Test;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by gmwang on 2018/7/25
 */
public class _0823BaseballGame {
    /**
     * You're now a baseball game point recorder.
     * 你现在是一个棒球游戏记录器
     * Given a list of strings, each string can be one of the 4 following types:
     * 给定字符串列表，每个字符串可以是以下4种类型之一
     * Integer (one round's score): Directly represents the number of points you get in this round.
     * 整数（一轮得分）：直接表示在这一轮中得到的点数
     * "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
     * “+”（一轮得分）：表示在这轮中得到的点是最后两个有效轮点的总和。
     * "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.
     * “D”（一轮得分）：表示在这一轮中得到的点是最后一个有效轮点的加倍数据。
     * "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.
     * “C”（一个操作，它不是回合的分数）：表示您得到的最后一轮无效的分数，应该被删除。
     * Each round's operation is permanent and could have an impact on the round before and the round after.
     * 每一轮的比赛都是永久性的，可能对比赛前和后一回合产生影响。
     * You need to return the sum of the points you could get in all the rounds.
     * 你需要返回所有回合中可以得到的积分之和。
     *
     * Example 1:
     * Input: ["5","2","C","D","+"]
     * Output: 30
     * Explanation:
     * Round 1: You could get 5 points. The sum is: 5.
     * Round 2: You could get 2 points. The sum is: 7.
     * Operation 1: The round 2's data was invalid. The sum is: 5.
     * Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
     * Round 4: You could get 5 + 10 = 15 points. The sum is: 30.
     * Example 2:
     * Input: ["5","-2","4","C","D","9","+","+"]
     * Output: 27
     * Explanation:
     * Round 1: You could get 5 points. The sum is: 5.
     * Round 2: You could get -2 points. The sum is: 3.
     * Round 3: You could get 4 points. The sum is: 7.
     * Operation 1: The round 3's data is invalid. The sum is: 3.
     * Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
     * Round 5: You could get 9 points. The sum is: 8.
     * Round 6: You could get -4 + 9 = 5 points. The sum is 13.
     * Round 7: You could get 9 + 5 = 14 points. The sum is 27.
     * Note:
     * The size of the input list will be between 1 and 1000.
     * Every integer represented in the list will be between -30000 and 30000.
     */
    /**
     * @param ops
     * @return
     */
    public int calPoints(String[] ops) {
        int sum = 0;
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < ops.length; i++) {
            String op = ops[i];
            //前两轮有效分总和
            if("+".equals(op)){
                int total = 0;
                for (int j = 0; j < 2; j++) {
                    total += ((LinkedList<Integer>) queue).get(j);
                }
                sum += total;
                ((LinkedList<Integer>) queue).addFirst(total);
            }else if("D".equals(op)){
                if(!queue.isEmpty()){
                    Integer last = ((LinkedList<Integer>) queue).peek() * 2;
                    //最后一轮有效加倍
                    sum += last;
                    ((LinkedList<Integer>) queue).addFirst(last);
                }
            }else if("C".equals(op)){
                if(!queue.isEmpty()){
                    //最后一轮无效
                    sum -= queue.poll();
                }
            }else {
                int score = Integer.parseInt(op);
                sum += score;
                ((LinkedList<Integer>) queue).addFirst(score);
            }
        }
        return  sum;
    }
    @Test
    public void test() {
//        String [] ops = {"5","2","C","D","+"};
//        String [] ops = {"5","-2","4","C","D","9","+","+"};
//        String [] ops = {"11482","C","-16664","-8297","+","D","19136","D","-10145","D","21701","+","20987","C","D","C","-17917","17151","273","C","19619","18087","D","-2040","-28447","-16785","-29805","C","C","D","-534","5898","-19769","13074","-17065","22139","28093","D","+","-22768","9116","C","C","-2035","D","25271","-8145","16575","+","-17996"};
//        String [] ops = {"8373","C","9034","-17523","D","1492","7558","-5022","C","C","+","+","-16000","C","+","-18694","C","C","C","-19377","D","-25250","20356","C","-1769","-8303","C","-25332","29884","C","+","C","-29875","-15374","C","+","14584","13773","+","17037","-28248","+","16822","D","+","+","-19357","-26593","-8548","4776","D","-8244","378","+","-19269","-25447","18922","-16782","2886","C","-17788","D","-22256","C","308","-9185","-19074","1528","28424","D","8658","C","7221","-13704","8995","-21650","22567","-29560","D","-9807","25632","6817","28654","D","-18574","C","D","20103","7875","C","9911","23951","C","D","C","+","18219","-20922","D","-24923"};
//        String [] ops = {"36","28","70","65","C","+","33","-46","84","C"};    //219
        String [] ops = {"5","-2","4","C","D","9","+","+"}; // 27
        int res = calPoints(ops);
        System.out.println(res);
    }

}
